<?php
/**
 * 分页组件
 * 用于在列表页显示分页导航
 * 
 * 使用方式:
 * 1. 在页面配置中添加分页配置
 * 2. 在页面模板中引入此组件
 * 
 * 示例配置:
 * $pageConfig['components']['pagination'] = [
 *     'total_items' => 100, // 总项目数
 *     'items_per_page' => 10, // 每页显示项目数
 *     'current_page' => 1, // 当前页码
 *     'max_visible_pages' => 5, // 最多显示的页码数
 *     'url_pattern' => '?page={page}', // URL模板，{page}会被替换为实际页码
 *     'show_first_last' => true, // 是否显示首页和末页按钮
 *     'show_prev_next' => true, // 是否显示上一页和下一页按钮
 *     'container_class' => 'flex justify-center py-6', // 容器样式类
 *     'item_class' => 'mx-1', // 页码项样式类
 *     'link_class' => 'px-3 py-2 rounded-md border transition-colors hover:bg-gray-100', // 链接样式类
 *     'active_class' => 'bg-primary text-white border-primary', // 当前页码样式类
 *     'disabled_class' => 'opacity-50 cursor-not-allowed', // 禁用按钮样式类
 *     'prev_text' => '<i class="fa fa-angle-left"></i>', // 上一页按钮文本
 *     'next_text' => '<i class="fa fa-angle-right"></i>', // 下一页按钮文本
 *     'first_text' => '<i class="fa fa-angle-double-left"></i>', // 首页按钮文本
 *     'last_text' => '<i class="fa fa-angle-double-right"></i>' // 末页按钮文本
 * ];
 */
$configKey = isset($configKey) ? $configKey : 'pagination';
// 获取分页配置，如果没有则使用默认配置
$data = array_merge([
    'section_id' => $configKey,
    'section_pretitle' => '',
    'section_title' => '分页导航',
    'section_subtitle' => '查看更多内容',
    'total_items' => 100,
    'items_per_page' => 10,
    'current_page' => 1,
    'max_visible_pages' => 5,
    'url_pattern' => '?page={page}',
    'show_first_last' => true,
    'show_prev_next' => true,
    'container_class' => 'flex justify-center py-6',
    'item_class' => 'mx-1',
    'link_class' => 'px-3 py-2 rounded-md border transition-colors hover:bg-gray-100',
    'active_class' => 'bg-primary text-white border-primary',
    'disabled_class' => 'opacity-50 cursor-not-allowed',
    'prev_text' => '<i class="fa fa-angle-left"></i>',
    'next_text' => '<i class="fa fa-angle-right"></i>',
    'first_text' => '<i class="fa fa-angle-double-left"></i>',
    'last_text' => '<i class="fa fa-angle-double-right"></i>'
], $pageConfig['components'][$configKey] ?? []);

// 从URL获取当前页码（如果有）
$currentPage = isset($_GET['page']) && is_numeric($_GET['page']) ? max(1, intval($_GET['page'])) : $data['current_page'];

// 计算总页数
$totalPages = max(1, ceil($data['total_items'] / $data['items_per_page']));
$currentPage = min($currentPage, $totalPages);

// 如果只有一页，不显示分页
if ($totalPages <= 1) {
    return;
}

// 生成页码范围
$maxVisiblePages = $data['max_visible_pages'];
$halfRange = floor($maxVisiblePages / 2);

if ($totalPages <= $maxVisiblePages) {
    // 如果总页数小于等于最大可见页数，显示所有页码
    $startPage = 1;
    $endPage = $totalPages;
} else {
    // 计算起始页码和结束页码
    $startPage = max(1, $currentPage - $halfRange);
    $endPage = min($totalPages, $startPage + $maxVisiblePages - 1);
    
    // 调整起始页码以确保显示足够的页码
    if ($endPage - $startPage + 1 < $maxVisiblePages) {
        $startPage = max(1, $endPage - $maxVisiblePages + 1);
    }
}

// 生成URL函数
function generatePageUrl($urlPattern, $page) {
    return str_replace('{page}', $page, $urlPattern);
}

// 开始输出分页HTML
?>
<div class="<?php echo $data['container_class']; ?>">
    <nav aria-label="Page navigation">
        <ul class="flex items-center">
            <!-- 首页按钮 -->
            <?php if ($data['show_first_last']): ?>
                <li class="<?php echo $data['item_class']; ?>">
                    <a 
                        href="<?php echo generatePageUrl($data['url_pattern'], 1); ?>"
                        class="<?php echo $data['link_class']; ?>
                        <?php echo $currentPage == 1 ? $data['disabled_class'] : ''; ?>"
                        aria-label="First page"
                        <?php echo $currentPage == 1 ? 'aria-disabled="true" tabindex="-1"' : ''; ?>>
                        <?php echo $data['first_text']; ?>
                    </a>
                </li>
            <?php endif; ?>
            
            <!-- 上一页按钮 -->
            <?php if ($data['show_prev_next']): ?>
                <li class="<?php echo $data['item_class']; ?>">
                    <a 
                        href="<?php echo generatePageUrl($data['url_pattern'], max(1, $currentPage - 1)); ?>"
                        class="<?php echo $data['link_class']; ?>
                        <?php echo $currentPage == 1 ? $data['disabled_class'] : ''; ?>"
                        aria-label="Previous page"
                        <?php echo $currentPage == 1 ? 'aria-disabled="true" tabindex="-1"' : ''; ?>>
                        <?php echo $data['prev_text']; ?>
                    </a>
                </li>
            <?php endif; ?>
            
            <!-- 页码按钮 -->
            <?php for ($i = $startPage; $i <= $endPage; $i++): ?>
                <li class="<?php echo $data['item_class']; ?>">
                    <a 
                        href="<?php echo generatePageUrl($data['url_pattern'], $i); ?>"
                        class="<?php echo $data['link_class']; ?>
                        <?php echo $i == $currentPage ? $data['active_class'] : ''; ?>"
                        aria-label="Page <?php echo $i; ?>"
                        aria-current="<?php echo $i == $currentPage ? 'page' : 'false'; ?>">
                        <?php echo $i; ?>
                    </a>
                </li>
            <?php endfor; ?>
            
            <!-- 下一页按钮 -->
            <?php if ($data['show_prev_next']): ?>
                <li class="<?php echo $data['item_class']; ?>">
                    <a 
                        href="<?php echo generatePageUrl($data['url_pattern'], min($totalPages, $currentPage + 1)); ?>"
                        class="<?php echo $data['link_class']; ?>
                        <?php echo $currentPage == $totalPages ? $data['disabled_class'] : ''; ?>"
                        aria-label="Next page"
                        <?php echo $currentPage == $totalPages ? 'aria-disabled="true" tabindex="-1"' : ''; ?>>
                        <?php echo $data['next_text']; ?>
                    </a>
                </li>
            <?php endif; ?>
            
            <!-- 末页按钮 -->
            <?php if ($data['show_first_last']): ?>
                <li class="<?php echo $data['item_class']; ?>">
                    <a 
                        href="<?php echo generatePageUrl($data['url_pattern'], $totalPages); ?>"
                        class="<?php echo $data['link_class']; ?>
                        <?php echo $currentPage == $totalPages ? $data['disabled_class'] : ''; ?>"
                        aria-label="Last page"
                        <?php echo $currentPage == $totalPages ? 'aria-disabled="true" tabindex="-1"' : ''; ?>>
                        <?php echo $data['last_text']; ?>
                    </a>
                </li>
            <?php endif; ?>
        </ul>
    </nav>
</div>